Option Strict On
Option Explicit On 

Public Class UTemplate
    Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lstAllColumns As System.Windows.Forms.ListBox
    Friend WithEvents lblAllColumns As System.Windows.Forms.Label
    Friend WithEvents lstKeyColumns As System.Windows.Forms.ListBox
    Friend WithEvents btnAdd As System.Windows.Forms.Button
    Friend WithEvents lblKeyColumns As System.Windows.Forms.Label
    Friend WithEvents btnRemove As System.Windows.Forms.Button
    Friend WithEvents btnUp As System.Windows.Forms.Button
    Friend WithEvents btnDown As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lstAllColumns = New System.Windows.Forms.ListBox
        Me.lblAllColumns = New System.Windows.Forms.Label
        Me.lstKeyColumns = New System.Windows.Forms.ListBox
        Me.btnAdd = New System.Windows.Forms.Button
        Me.lblKeyColumns = New System.Windows.Forms.Label
        Me.btnRemove = New System.Windows.Forms.Button
        Me.btnUp = New System.Windows.Forms.Button
        Me.btnDown = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'lstAllColumns
        '
        Me.lstAllColumns.Location = New System.Drawing.Point(0, 16)
        Me.lstAllColumns.Name = "lstAllColumns"
        Me.lstAllColumns.Size = New System.Drawing.Size(120, 95)
        Me.lstAllColumns.TabIndex = 0
        '
        'lblAllColumns
        '
        Me.lblAllColumns.Location = New System.Drawing.Point(0, 0)
        Me.lblAllColumns.Name = "lblAllColumns"
        Me.lblAllColumns.Size = New System.Drawing.Size(120, 16)
        Me.lblAllColumns.TabIndex = 12
        Me.lblAllColumns.Text = "All Columns:"
        '
        'lstKeyColumns
        '
        Me.lstKeyColumns.Location = New System.Drawing.Point(160, 16)
        Me.lstKeyColumns.Name = "lstKeyColumns"
        Me.lstKeyColumns.Size = New System.Drawing.Size(120, 95)
        Me.lstKeyColumns.TabIndex = 1
        '
        'btnAdd
        '
        Me.btnAdd.Location = New System.Drawing.Point(120, 16)
        Me.btnAdd.Name = "btnAdd"
        Me.btnAdd.Size = New System.Drawing.Size(40, 23)
        Me.btnAdd.TabIndex = 8
        Me.btnAdd.Text = ">"
        '
        'lblKeyColumns
        '
        Me.lblKeyColumns.Location = New System.Drawing.Point(160, 0)
        Me.lblKeyColumns.Name = "lblKeyColumns"
        Me.lblKeyColumns.Size = New System.Drawing.Size(120, 16)
        Me.lblKeyColumns.TabIndex = 13
        Me.lblKeyColumns.Text = "Key Columns:"
        '
        'btnRemove
        '
        Me.btnRemove.Location = New System.Drawing.Point(120, 40)
        Me.btnRemove.Name = "btnRemove"
        Me.btnRemove.Size = New System.Drawing.Size(40, 23)
        Me.btnRemove.TabIndex = 9
        Me.btnRemove.Text = "<"
        '
        'btnUp
        '
        Me.btnUp.Location = New System.Drawing.Point(280, 16)
        Me.btnUp.Name = "btnUp"
        Me.btnUp.Size = New System.Drawing.Size(48, 23)
        Me.btnUp.TabIndex = 10
        Me.btnUp.Text = "Up"
        '
        'btnDown
        '
        Me.btnDown.Location = New System.Drawing.Point(280, 40)
        Me.btnDown.Name = "btnDown"
        Me.btnDown.Size = New System.Drawing.Size(48, 23)
        Me.btnDown.TabIndex = 11
        Me.btnDown.Text = "Down"
        '
        'UPrimaryKey
        '
        Me.Controls.Add(Me.lblKeyColumns)
        Me.Controls.Add(Me.lblAllColumns)
        Me.Controls.Add(Me.btnDown)
        Me.Controls.Add(Me.btnUp)
        Me.Controls.Add(Me.btnRemove)
        Me.Controls.Add(Me.btnAdd)
        Me.Controls.Add(Me.lstKeyColumns)
        Me.Controls.Add(Me.lstAllColumns)
        Me.Name = "UPrimaryKey"
        Me.Size = New System.Drawing.Size(328, 112)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Event ValueChanged As EventHandler

    Private Sub UPrimaryKey_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub lstAllColumns_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstAllColumns.DoubleClick
        btnAdd.PerformClick()
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        lstKeyColumns.Items.Add(lstAllColumns.SelectedItem)

        btnRemove.Enabled = True
        If lstKeyColumns.Items.Count > 1 Then
            btnUp.Enabled = True
            btnDown.Enabled = True
        End If

        OnValueChanged(Me, New EventArgs)
    End Sub
    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        lstKeyColumns.Items.Remove(lstKeyColumns.SelectedItem)

        If lstKeyColumns.Items.Count = 0 Then btnRemove.Enabled = False
        If lstKeyColumns.Items.Count < 2 Then
            btnUp.Enabled = False
            btnDown.Enabled = False
        End If

        OnValueChanged(Me, New EventArgs)
    End Sub
    Private Sub lstKeyColumns_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstKeyColumns.DoubleClick
        btnRemove.PerformClick()
    End Sub
    Private Sub lstKeyColumns_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstKeyColumns.SelectedIndexChanged
        btnUp.Enabled = lstKeyColumns.Items.IndexOf(lstKeyColumns.SelectedItem) <> 0
        btnDown.Enabled = lstKeyColumns.Items.IndexOf(lstKeyColumns.SelectedItem) <> lstKeyColumns.Items.Count - 1
    End Sub
    Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
        Dim obj As Object = lstKeyColumns.SelectedItem
        Dim i As Integer = lstKeyColumns.Items.IndexOf(lstKeyColumns.SelectedItem)
        lstKeyColumns.Items.Remove(lstKeyColumns.SelectedItem)
        lstKeyColumns.Items.Insert(i - 1, obj)
        obj = Nothing

        OnValueChanged(Me, New EventArgs)
    End Sub
    Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click
        Dim obj As Object = lstKeyColumns.SelectedItem
        Dim i As Integer = lstKeyColumns.Items.IndexOf(lstKeyColumns.SelectedItem)
        lstKeyColumns.Items.Remove(lstKeyColumns.SelectedItem)
        lstKeyColumns.Items.Insert(i + 1, obj)
        obj = Nothing

        OnValueChanged(Me, New EventArgs)
    End Sub

    Protected Sub OnValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        RaiseEvent ValueChanged(Me, New EventArgs)
    End Sub

    Public Sub Read(ByVal table As Model10.Table)
        Dim dc As DataColumn
        '-------------------
        lstAllColumns.Items.Clear()
        For Each dc In table.DataTable.Columns
            lstAllColumns.Items.Add(dc)
        Next : dc = Nothing
        '-------------------
        lstKeyColumns.Items.Clear()
        For Each dc In table.DataTable.PrimaryKey
            lstKeyColumns.Items.Add(dc)
        Next : dc = Nothing
        '-------------------
        If lstAllColumns.Items.Count = 0 Then btnAdd.Enabled = False
        If lstKeyColumns.Items.Count = 0 Then btnRemove.Enabled = False
        If lstKeyColumns.Items.Count < 2 Then
            btnUp.Enabled = False
            btnDown.Enabled = False
        End If
    End Sub
    Public Sub Write(ByVal table As Model10.Table)
        Dim pk() As DataColumn

        Dim i As Integer
        For i = 0 To lstKeyColumns.Items.Count - 1
            ReDim Preserve pk(i) : pk(i) = CType(lstKeyColumns.Items(i), DataColumn)
        Next

        table.DataTable.PrimaryKey = pk : pk = Nothing
    End Sub
End Class
